// 获取用户信息 渲染input框
let user_id

function getUserInfo() {
    axios({
            method: 'get',
            url: '/my/userinfo',
        })
        .then(res => {
            let { id, username, nickname, email } = res.data.data
            user_id = id
            $('#username').val(username)
            $('#nickname').val(nickname)
            $('#email').val(email)
        })
}
getUserInfo()

// 表单验证
let form = layui.form
form.verify({
    nickname: function(value, item) {
        let reg = /^[\u4e00-\u9fa5a-z\d_-]{2,10}$/i
        if (!reg.test(value)) {
            return '昵称可以是2-10位汉子,字母,数字_,-的组合'
        }
    }
})

// 提交修改
$('#submit').on('click', function(e) {
    e.preventDefault()
    axios({
            method: 'post',
            url: '/my/userinfo',
            data: `id=${user_id}&nickname=${$('#nickname').val()}&email=${$('#email').val()}`
        })
        .then(res => {
            // 父窗口(index.js)里的getUserInfo() 方法
            window.parent.getUserInfo()
        })
})

// 重置按钮
$('#reset').on('click', function() {
    getUserInfo()
})